package ejb.facade;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ejb.dao.EventDAO;
import ejb.dao.InvitationDAO;
import ejb.entities.User;
import ejb.entities.Event;
import ejb.entities.Invitation;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import web.beans.Login;


@Stateless
public class EventFacadeImp implements EventFacade{
    
        @EJB 
	private EventDAO eventDAO;
        
        @EJB 
	private InvitationFacade invFacade;
        
        @Inject
	private Login login;
	
	@Override
	public void save(Event s) {
		// TODO Auto-generated method stub
		eventDAO.save(s);
	}

	@Override
	public Event update(Event s) {
		// TODO Auto-generated method stub
		return eventDAO.update(s);
	}

	@Override
	public void delete(Event s) {
		// TODO Auto-generated method stub
		eventDAO.delete(s);
	}

	@Override
	public Event find(int entityID) {
		// TODO Auto-generated method stub
		return eventDAO.find(entityID);
	}

	@Override
	public List<Event> findAll() {
		// TODO Auto-generated method stub
		return eventDAO.findAll();
	}
        
        @Override
	public List<Event> findAllAfterToday() {
                List <Event> nextEvent;
                nextEvent = new ArrayList();
                DateFormat stringFormat = new SimpleDateFormat("dd/MM/yyyy");
                Date todayDate = login.getToday();   
                for(Event ev : eventDAO.findAll()){
                      if(ev.getDateTimeStart().after(todayDate)){
                          nextEvent.add(ev);
                      }                
                }
                return nextEvent;
	}
        
        @Override
	public Event findEventById(int id) {
		return eventDAO.findEventById(id);
	}
        
        @Override
	public List<Event> findEventByCreator(User us) {
		return eventDAO.findEventByCreator(us);
	}
        
        @Override
	public List<Event> findEventByCreatorAfterToday(User us){
            List <Event> nextEvent;
                nextEvent = new ArrayList();
                DateFormat stringFormat = new SimpleDateFormat("dd/MM/yyyy");
                Date todayDate = login.getToday();   
                for(Event ev : eventDAO.findEventByCreator(us)){
                      if(ev.getDateTimeStart().after(todayDate)){
                          nextEvent.add(ev);
                      }                
                }
                return nextEvent;
        }
        
        
        @Override
	public List<Event> findEventWithBadWeather() {
	    List<Event> badEvent;	
            badEvent = new ArrayList();
            for(Event ev : findEventByCreatorAfterToday(login.getUser())){
                //Bad weather notification is only for outdoor Events
                if(ev.getIdWeather().isABadWeather() && ev.getType()=='O'){
                    badEvent.add(ev);
                }
            }
            for(Invitation inv : invFacade.findInvitationByUserAfterToday(login.getUser()))
            {
                if(inv.getEvent().getIdWeather().isABadWeather() && inv.getEvent().getType()=='O'){
                    badEvent.add(inv.getEvent());
                }
            }
            return badEvent;
	}
        
        @Override
	public List<Event> findMyEventWithBadWeatherOneDayBeforeToday() {
            DateFormat stringFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date tommorowDate = login.getTomorrow();
            Date todayDate = login.getToday();
	    List<Event> badEvent;	
            badEvent = new ArrayList();
             for(Event ev : findEventByCreatorAfterToday(login.getUser())){
                String forecastStr = stringFormat.format(ev.getDateTimeEnd());
                Date eventDate = new Date();
                try {
                    eventDate = stringFormat.parse(forecastStr);
                } catch (ParseException ex) {
                      Logger.getLogger(WeatherFacadeImp.class.getName()).log(Level.SEVERE, null, ex);
                }
                if((eventDate.equals(login.fromDateTimetoDate(tommorowDate)) || 
                        eventDate.equals(login.fromDateTimetoDate(todayDate))) &&
                           ev.getDateTimeEnd().after(todayDate) &&
                            ev.getType()=='O' && ev.getIdWeather().isABadWeather()){
                    badEvent.add(ev);
                }
            }
            
            for(Invitation inv : invFacade.findInvitationByUserAfterToday(login.getUser()))
            {
                if(!inv.getInvstate().equals("P")){
                    String forecastStr = stringFormat.format(inv.getEvent().getDateTimeEnd());
                    Date eventDate = new Date();
                    try {
                        eventDate = stringFormat.parse(forecastStr);
                    } catch (ParseException ex) {
                          Logger.getLogger(WeatherFacadeImp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if(eventDate.equals(login.fromDateTimetoDate(todayDate)) && inv.getEvent().getType()=='O' && inv.getEvent().getIdWeather().isABadWeather()){
                        badEvent.add(inv.getEvent());
                    }
                }
            }
            return badEvent;
	}   
        
        @Override
        public List<Event> findMyEventOfTomorrow() {
            DateFormat stringFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date tomorrowDate = login.getTomorrow();   
	    List<Event> badEvent;	
            badEvent = new ArrayList();
            for(Event ev : findEventByCreatorAfterToday(login.getUser())){
                String forecastStr = stringFormat.format(ev.getDateTimeEnd());
                Date eventDate = new Date();
                try {
                    eventDate = stringFormat.parse(forecastStr);
                } catch (ParseException ex) {
                      Logger.getLogger(WeatherFacadeImp.class.getName()).log(Level.SEVERE, null, ex);
                }
                if(eventDate.equals(login.fromDateTimetoDate(tomorrowDate))){
                    badEvent.add(ev);
                }
            }
            
            for(Invitation inv : invFacade.findInvitationByUserAfterToday(login.getUser()))
            {
                if(!inv.getInvstate().equals("P")){
                    String forecastStr = stringFormat.format(inv.getEvent().getDateTimeEnd());
                    Date eventDate = new Date();
                    try {
                        eventDate = stringFormat.parse(forecastStr);
                    } catch (ParseException ex) {
                          Logger.getLogger(WeatherFacadeImp.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if(eventDate.equals(login.fromDateTimetoDate(tomorrowDate))){
                        badEvent.add(inv.getEvent());
                    }
                }
            }
                    
            return badEvent;
	}   
               
}

